package com.zking.service.impl;

import com.zking.dao.domain.DateAndDoctor;
import com.zking.dao.domain.Department;
import com.zking.dao.domain.Doc_schedule;
import com.zking.dao.domain.Doctor;
import com.zking.dao.mapper.DoctorMapper;
import com.zking.service.DoctorService;
import com.zking.utils.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.swing.plaf.synth.SynthSpinnerUI;
import java.io.File;
import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.List;

@Service
public class DoctorServiceImpl implements DoctorService {

    @Autowired
    private DoctorMapper doctorMapper;

    @Override
    public List<Doctor> queryDoctors() {
        return doctorMapper.queryDoctors();
    }

    @Override
    public int deleteDoctor(int id) {
        return doctorMapper.deleteDoctor(id);
    }

    @Override
    public int insertDoctor(Doctor doctor) {
        return doctorMapper.insertDoctor(doctor);
    }

    @Override
    public int updateDoctor(Doctor doctor) {
        return doctorMapper.updateDoctor(doctor);
    }

    @Override
    public Doctor queryDoctor(int id) {
        return doctorMapper.queryDoctor(id);
    }

    @Override
    public List<Department> queryDepartments() {
        return doctorMapper.queryDepartments();
    }

    @Override
    public String upload(MultipartFile file, String filePath) {
        try {
            String fileName = file.getOriginalFilename();
            byte[] file1 = file.getBytes();
            //设置文件上传路径
            File targetFile = new File(filePath);
            if (!targetFile.exists()) {
                targetFile.mkdirs();
            }
            FileOutputStream out = null;
            out = new FileOutputStream(filePath + fileName);
            out.write(file1);
            out.flush();
            out.close();
            return filePath + fileName;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }


    @Override
    public List<Doctor> department_doctor(int deptid) {
        return doctorMapper.department_doctor(deptid);
    }

    @Override
    public boolean updateSchedule(List<DateAndDoctor> dateAndDoctors,int dept_id,String update_time) {
        for (int i = 0;i<dateAndDoctors.size()-1;i++){
            for (int j = i+1;j<dateAndDoctors.size();j++){
               if (dateAndDoctors.get(i).getDate().equals(dateAndDoctors.get(j).getDate())){
                   if (dateAndDoctors.get(i).getDoctorId()==dateAndDoctors.get(j).getDoctorId()){
                       dateAndDoctors.remove(j);
                       dateAndDoctors.get(i).setTime_interval(3);
                   }
               }
            }
        }
        System.out.println(dateAndDoctors);
        System.out.println("修改日期："+update_time);
        List<Doc_schedule> doc_schedules = this.query_Schedules(dept_id);
        System.out.println("doc_schedules="+doc_schedules);
        boolean flag = false;
        for (DateAndDoctor dateAndDoctor : dateAndDoctors) {
            for (Doc_schedule doc_schedule : doc_schedules) {
                if (doc_schedule.getDaytime().equals(update_time)) {
                    if (doc_schedule.getDid() == dateAndDoctor.getDoctorId()) {
                        flag = doctorMapper.delete_Schedule(dateAndDoctor.getDoctorId(), dateAndDoctor.getDate());
                    }
                }
            }
            if ( dateAndDoctor.getTime_interval() == 1){
                flag =  doctorMapper.addSchedule_morning(dateAndDoctor.getDoctorId(),dateAndDoctor.getDoctor_name(),dept_id,dateAndDoctor.getDate(),dateAndDoctor.getTime_interval());
            }else if (dateAndDoctor.getTime_interval() == 2){
                flag = doctorMapper.addSchedule_afternoon(dateAndDoctor.getDoctorId(),dateAndDoctor.getDoctor_name(),dept_id,dateAndDoctor.getDate(),dateAndDoctor.getTime_interval());
            }else if (dateAndDoctor.getTime_interval() == 3){
                flag = doctorMapper.addSchedule(dateAndDoctor.getDoctorId(),dateAndDoctor.getDoctor_name(),dept_id,dateAndDoctor.getDate(),dateAndDoctor.getTime_interval());
            }
            if (flag == false){
                return flag;
            }
        }
        return flag;
    }

    @Override
    public List<Doc_schedule> query_Schedules(int dept_id) {
        DateUtil dateUtil = new DateUtil();
        List<String> dates = dateUtil.getWeekDay();
        List<Doc_schedule> doc_schedules = doctorMapper.queryDoc_schedules(dates.get(0),dates.get(1),dates.get(2),dates.get(3),dates.get(4),dates.get(5),dates.get(6),dates.get(7),dept_id);
        List<Doc_schedule> doc_schedules1 = new ArrayList<>();
        Doc_schedule doc_schedule1 = new Doc_schedule(0,"0",0,"0",0,0,0,0,0,0,0,0,0);
        for (Doc_schedule doc_schedule : doc_schedules) {
            if (doc_schedule.getDept_id() == dept_id){
                doc_schedules1.add(doc_schedule);
            }
        }
        doc_schedules1.add(doc_schedule1);
        return doc_schedules1;
    }

}
